package com.bootx.officialAccount.job;

import com.bootx.officialAccount.entity.AccessToken;
import com.bootx.officialAccount.entity.OfficialAccount;
import com.bootx.officialAccount.service.AccessTokenService;
import com.bootx.officialAccount.service.OfficialAccountService;
import com.bootx.officialAccount.util.OfficialAccountUtils;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author black
 */
@Component
public class AccessTokenJob {


    @Resource
    private AccessTokenService accessTokenService;

    @Resource
    private OfficialAccountService officialAccountService;

    @Scheduled(fixedRate = 1000 * 60 * 2)
    public void execute() {
        List<OfficialAccount> officialAccounts = officialAccountService.findAll();
        for (OfficialAccount officialAccount : officialAccounts) {
            if(accessTokenService.expire(officialAccount)){
                AccessToken accessToken = OfficialAccountUtils.getToken();
                accessTokenService.createOrUpdate(officialAccount, accessToken);
            }
        }
    }

}
